package com.netposa.service.impl;

import com.google.gson.Gson;
import com.netposa.config.ImportPoliceConfig;
import com.netposa.mapper.db0.PoliceMapper;
import com.netposa.mapper.oracle.OrcPoliceMapper;
import com.netposa.model.PoliceModel;
import com.netposa.service.PoliceImportService;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

@Service
public class PoliceImportServiceImpl implements PoliceImportService {
   private static final Logger logger = Logger.getLogger(PoliceImportServiceImpl.class);
   @Autowired
   private OrcPoliceMapper orcPoliceMapper;
   @Autowired
   private PoliceMapper policeMapper;
   @Autowired
   private JmsMessagingTemplate jmsMessagingTemplate;
   @Autowired
   private StringRedisTemplate stringRedisTemplate;
   @Autowired
   private Gson gson;
   @Autowired
   private ImportPoliceConfig config;
   private final String redisStartTime = "POLICE_IMPORT:startTime";

   public void importPolice() {
      try {
         Long time = System.currentTimeMillis();
         Integer index = 1;
         boolean hasAddData = true;

         while(hasAddData) {
            logger.info("#####开始导入警情, 页码：" + index + "#####");
            hasAddData = this.importData();
            logger.info("#####导入警情完成, 页码：" + index + "#####");
            index = index + 1;
            Thread.sleep((long)this.config.getSleep());
         }

         logger.info("#####结束导入警情#####，耗时：" + (System.currentTimeMillis() - time));
      } catch (Exception var6) {
         logger.error(var6.getMessage(), var6);
      }

   }

   private boolean importData() {
      String startTime = this.stringRedisTemplate.opsForValue().get(this.redisStartTime);
      if (StringUtils.isEmpty(startTime)) {
         startTime = this.config.getDefaultStartTime();
      }

      List<PoliceModel> policeModels = this.orcPoliceMapper.getPoliceModels(startTime, this.config.getPageSize());
      if (CollectionUtils.isEmpty(policeModels)) {
         return false;
      } else {
         try {
            this.policeMapper.batchInsert(policeModels);
            if (this.config.getMqOpen()) {
               Iterator<PoliceModel> var3 = policeModels.iterator();

               while(var3.hasNext()) {
                  PoliceModel policeModel = var3.next();
                  this.jmsMessagingTemplate.convertAndSend(this.config.getMqTopic(), this.gson.toJson(policeModel));
               }
            }

            Comparator<PoliceModel> comparator = Comparator.comparing(PoliceModel::getGxsj);
            policeModels.sort(comparator.reversed());
            String updateTime = policeModels.get(0).getGxsj();
            this.stringRedisTemplate.opsForValue().set(this.redisStartTime, updateTime);
         } catch (Exception var5) {
            logger.error(var5.getMessage());
         }

         return true;
      }
   }
}
